// function audioCheck(params) {
//     console.log("ok");
//     function onError() {
//         // mystatus.innerHTML = "获取音频时好像出了点问题。"
//         console.log("获取音频时好像出了点问题。");
//     console.log(" onSuccess");

//     }
//     function onSuccess(stream) {
//     console.log(" onSuccess");

//         audioContext = window.AudioContext || window.webkitAudioContext;
//         context = new audioContext(); //创建一个管理、播放声音的对象
//         liveSource = context.createMediaStreamSource(stream); //将麦克风的声音输入这个对象
//         var levelChecker = context.createScriptProcessor(4096, 1, 1); //创建一个音频分析对象，采样的缓冲区大小为4096，输入和输出都是单声道
//         liveSource.connect(levelChecker); //将该分析对象与麦克风音频进行连接
//         levelChecker.connect(context.destination)
//         levelChecker.onaudioprocess = function (e) { //开始处理音频
//             console.log("onaudioprocess==",e);
//             var buffer = e.inputBuffer.getChannelData(0); //获得缓冲区的输入音频，转换为包含了PCM通道数据的32位浮点数组
//             //创建变量并迭代来获取最大的音量值
//             var maxVal = 0;
//             for (var i = 0; i < buffer.length; i++) {
//                 if (maxVal < buffer[i]) {
//                     maxVal = buffer[i];
//                 }
//             }
//             //显示音量值
//             // mystatus.innerHTML = "您的音量值：" + Math.round(maxVal * 100);
//             if (maxVal > .5) {
//                 //当音量值大于0.5时，显示“声音太响”字样，并断开音频连接
//                 // mystatus.innerHTML = "您的声音太响了!!";
//                 liveSource.disconnect(levelChecker);
//             }
//             console.log("maxVal==",maxVal);
//         };
//     }
//     navigator.getUserMedia({
//         audio: true
//     }, onSuccess, onError);
// }

function beginDetect() {
  console.log("start");
  document.querySelector(".music-icon").classList.remove("rotate");
  document.querySelector(".music").pause();
  let wrap = document.getElementById("status-wrap");
  wrap.style.visibility = "visible";
  let mystatus = document.getElementById("status");
  let statusBar = document.getElementById("status-bar");
  let barInner = document.getElementById("bar-inner");
  let innerIcon = document.getElementById("inner-icon");
  // let audioContext = new(window.AudioContext || window.webkitAudioContext)()
  let AudioContext = window.AudioContext || window.webkitAudioContext;
  let audioContext = new AudioContext();
  let mediaStreamSource = null;
  let scriptProcessor = null;
  let candleFires = document.getElementsByClassName("fuego");
  let handleBtn = document.getElementById("btn-start");
  let frozenVal = 0;
  let doneVal = window._urlParams.max || 70;
  const handleFire = (isShow) => {
    if (candleFires) {
      for (let index = 0; index < candleFires.length; index++) {
        candleFires[index].style.visibility = isShow ? "visible" : "hidden";
      }
    }
  };
  statusBar.style.visibility = "visible";
  handleFire(true);
  if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
    // 获取用户的 media 信息
    navigator.mediaDevices
      .getUserMedia({
        audio: true,
      })
      .then((stream) => {
        handleBtn.innerHTML = "加油~ ψ(｀∇´)ψ";
        // 将麦克风的声音输入这个对象
        mediaStreamSource = audioContext.createMediaStreamSource(stream);
        // 创建一个音频分析对象，采样的缓冲区大小为4096，输入和输出都是单声道
        scriptProcessor = audioContext.createScriptProcessor(4096, 1, 1);
        // 将该分析对象与麦克风音频进行连接
        mediaStreamSource.connect(scriptProcessor);
        // 此举无甚效果，仅仅是因为解决 Chrome 自身的 bug
        scriptProcessor.connect(audioContext.destination);

        // 开始处理音频
        scriptProcessor.onaudioprocess = function (e) {
          // 获得缓冲区的输入音频，转换为包含了PCM通道数据的32位浮点数组
          let buffer = e.inputBuffer.getChannelData(0);
          // 获取缓冲区中最大的音量值
          let maxVal = Math.max.apply(Math, buffer);

          // 显示音量值
          let standarVal = Math.round(maxVal * 100);
          mystatus.innerHTML = "您的力度：" + standarVal;
          barInner.style.height = `${(standarVal / doneVal) * 100}%`;
          innerIcon.style.bottom = `${(standarVal / doneVal) * 100}%`;
          if (standarVal >= doneVal) {
            frozenVal = standarVal;
            if (window.flower) {
              window.flower();
            }
            audioContext.close().then(() => {
              mystatus.innerHTML = "您的力度：" + frozenVal;
              handleBtn.innerHTML = "不过瘾，再吹一次~";
              handleFire(false);
              barInner.style.height = 0;
              statusBar.style.visibility = "hidden";
              wrap.style.visibility = "hidden";
            });
          }
        };
      })
      .catch((error) => {
        mystatus.innerHTML =
          error +
          "\n" +
          "获取麦克风输入时好像出了点问题，退出后重新访问或者换个浏览器试试吧~";
      });
  } else {
    console.log("err");
    document.getElementById("status-wrap").innerHTML =
      "您的设备不支持使用麦克风，换个浏览器打开试试吧~";
  }
}
